package com.example.springboot.mapper;


import com.example.springboot.entity.IncorrectQuestion;
import com.example.springboot.entity.vo.IncorrectQuestionVo;
import com.example.springboot.entity.vo.MistakeStatisticsVo;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface IncorrectQuestionMapper {

    @Insert("INSERT INTO incorrect_questions(user_id, question_id) VALUES(#{userId}, #{questionId})")
    void insertIncorrectQuestion(IncorrectQuestion incorrectQuestion);

    @Select("""
    SELECT 
        iq.id AS id,
        iq.user_id AS userId,
        iq.question_id AS questionId,
        q.post_id AS postId,
        q.question AS question,
        q.options AS options,
        q.correct_answer AS correctAnswer,
        q.created_at AS questCreatedAt,
        q.updated_at AS questUpdatedAt,
        iq.created_at AS createdAt,
        iq.updated_at AS updatedAt
    FROM 
        incorrect_questions iq
    JOIN 
        questions q 
    ON 
        iq.question_id = q.id
    WHERE 
        iq.user_id = #{userId}
        AND iq.created_at BETWEEN #{startTime} AND #{endTime}
    LIMIT #{size} OFFSET #{offset}
""")
    List<IncorrectQuestionVo> getIncorrectQuestionsByUserIdAndTimeRange(
            @Param("userId") int userId,
            @Param("startTime") Timestamp startTime,
            @Param("endTime") Timestamp endTime,
            @Param("offset") int offset,
            @Param("size") int size
    );




    @Delete("DELETE FROM incorrect_questions WHERE id = #{id}")
    void deleteIncorrectQuestion(int id);

    MistakeStatisticsVo getTotalIncorrectQuestions(int userId);
}
